package tool

import (
	"MoSkeleton/models/tool"
	"MoSkeleton/services/base"
	"gorm.io/gorm"
)

type GenTemplateFileService struct {
	base.BaseService
}

func (g *GenTemplateFileService) GetTemplateFilesByClassifyId(classifyId uint64) ([]string, error) {
	var templateFiles []string
	err := g.GetDb(nil).Model(&tool.GenTemplateFiles{}).Where("classify_id = ?", classifyId).Pluck("file_name", &templateFiles).Error
	return templateFiles, err
}

func (g *GenTemplateFileService) AddTemplateFiles(tx *gorm.DB, fileName []string, classifyId uint64, userename string) error {
	for _, v := range fileName {
		err := g.GetDb(tx).Create(&tool.GenTemplateFiles{
			ClassifyId: classifyId,
			FileName:   v,
			CreatedBy:  userename,
			UpdatedBy:  userename,
		}).Error
		if err != nil {
			return err
		}
	}
	return nil
}

func (g *GenTemplateFileService) RemoveTemplateFilesByClassifyIds(tx *gorm.DB, classifyIds *[]uint64) error {
	return g.GetDb(tx).Where("classify_id in ?", *classifyIds).Delete(&tool.GenTemplateFiles{}).Error
}
